# -*- coding: utf-8 -*-
# @Time : 2025/10/23 15:06:30
# @Desc : 创建系统托盘图标
# @Author : Sean
# @File   : create_tray.py
import pystray
from PIL import Image
from .common import get_resource_path
import threading


class TrayIcon:
    """创建系统托盘图标"""

    def __init__(self, title, window):
        self.title = title
        self.window = window
        self.icon = get_resource_path("icon/logo.ico")

    def create_icon(self):
        """获取图标"""
        return Image.open(self.icon)

    def create_menu(self):
        """创建菜单"""
        menu = pystray.Menu(
            pystray.MenuItem("打开", self.show_window),
            pystray.MenuItem("退出", self.quit_window),
        )
        return menu

    def show_window(self):
        """显示窗口"""
        self.window.show()

    def quit_window(self):
        """退出程序"""
        self.tray.stop()
        self.window.destroy()

    def create_tray(self):
        """创建托盘图标"""
        self.tray = pystray.Icon(
            self.title, self.create_icon(), menu=self.create_menu()
        )
        return self.tray

    def start_tray(self):
        """托盘图标start"""
        self.tray.run()

    def run(self):
        """运行托盘图标"""
        threading.Thread(target=self.start_tray, daemon=True).start()
